//
//  VisitorLoginView.swift
//  艾小夕の微小博
//
//  Created by 艾小新 on 15/10/16.
//  Copyright © 2015年 艾小新. All rights reserved.
//

import UIKit


/// 设置代理方法
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func willRegister()
    func willLogin()
}

class VisitorLoginView: UIView {
    
    weak var delegate: VisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
      
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 提供外界接口设置访客视图
    ///
    /// - parameter ishome: 是主页吗
    /// - parameter imageName:  页面图片
    /// - parameter message:  页面title
    func setupViewInfo(isHome: Bool, imageName: String, message: String) {
        
        descLabel.text = message
        
        iconView.image = UIImage(named: imageName)
        
        homeIconView.hidden = !isHome
        
        isHome ? startAnim() : sendSubviewToBack(shadowView)
    }

    
    
    /// 开启主页转轮动画
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 20
        
        anim.repeatCount = MAXFLOAT
        
        anim.toValue = 2 * M_PI
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    /// 添加控件并自动布局
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        // 添加控件
        addSubview(iconView)
        addSubview(shadowView)
        addSubview(homeIconView)
        addSubview(descLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : shadowView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": shadowView, "regButton": registerBtn]))
        
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -7))
        
        descLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
   
    }
    
    // MARK:懒加载控件
    
    /// 转圈图标
    private lazy var iconView: UIImageView = {
        
       let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iconView
        
    }()
    
     /// 灰色遮罩
    private lazy var shadowView: UIImageView = {
        
       let shadowView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return shadowView
        
    }()
    
    /// 中间图标
    private lazy var homeIconView: UIImageView = {
        
        let homeIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return homeIconView
        
    }()
    
    /// 文字描述
    private lazy var descLabel: UILabel = {
        
        let descLabel = UILabel()
        
        descLabel.text = "关注我关注我关注关注关注关注关注关注"
        
        descLabel.textAlignment = NSTextAlignment.Center
        
        descLabel.numberOfLines = 0
        
        descLabel.textColor = UIColor.darkGrayColor()
        
        descLabel.font = UIFont.systemFontOfSize(15)
        
        return descLabel
    
    }()
    
    /// 左侧注册按钮
    private lazy var registerBtn: UIButton = {
       
        let registerBtn = UIButton(type: UIButtonType.Custom)
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registerBtn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerBtn
        
    }()
    
    /// 右侧登陆按钮
    private lazy var loginBtn: UIButton = {
        
        let loginBtn = UIButton(type: UIButtonType.Custom)
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginBtn.setTitle("登陆", forState: UIControlState.Normal)
        
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        loginBtn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
        
        }()
    
    // MARK: 按钮单击时间
    
    func register() {
        
        delegate?.willRegister()
    }
    
    func login() {
        
        delegate?.willLogin()
    }
    
    
}
